/**
 * 
 */
package br.com.livraria.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.com.livraria.dao.AbstractDao;
import br.com.livraria.dao.EditoraDao;
import br.com.livraria.vo.Editora;

/**
 * @author Jeferson
 *
 */
public class EditoraDaoImpl extends AbstractDao implements EditoraDao {
	/* (non-Javadoc)
	 * @see br.com.livraria.dao.EditoraDao#incluir(br.com.livraria.vo.Editora)
	 */
	@Override
	public void incluir(Editora editora) {
		
		try {
			Connection con = open();
			
			PreparedStatement preparedStatement = 
					con.prepareStatement("INSERT INTO editora (nome) VALUES (?)");
			
			preparedStatement.setString(1, editora.getNome());
			
			preparedStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}

	}

	/* (non-Javadoc)
	 * @see br.com.livraria.dao.EditoraDao#alterar(br.com.livraria.vo.Editora)
	 */
	@Override
	public void alterar(Editora editora) {
	
		try {
			Connection con = open();
			
			PreparedStatement preparedStatement =
					con.prepareStatement(" UPDATE editora SET nome = ?  WHERE id = ? ");
			
			preparedStatement.setString(1, editora.getNome());
			preparedStatement.setInt(2, editora.getId());
			
			preparedStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		
		
	

	}

	/* (non-Javadoc)
	 * @see br.com.livraria.dao.EditoraDao#excluir(java.lang.Integer)
	 */
	@Override
	public void excluir(Integer id) {
		try {
			Connection con = open();
			
			PreparedStatement preparedStatement =
					con.prepareStatement(" DELETE FROM editora WHERE id = ? ");
			
			preparedStatement.setInt(1, id);
			
			preparedStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}

	/* (non-Javadoc)
	 * @see br.com.livraria.dao.EditoraDao#consultarPorId(java.lang.Integer)
	 */
	@Override
	public Editora consultarPorId(Integer id) {
		Editora editora = new Editora();
		try {
			Connection con = open();
			PreparedStatement pst = con.prepareStatement("SELECT * FROM editora WHERE id = ?");
			
			pst.setInt(1, id);
					
			ResultSet rs = pst.executeQuery();
			
			while (rs.next()) {
				editora.setId(rs.getInt("id"));
				editora.setNome(rs.getString("nome"));				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		
		return editora;
	}

	/* (non-Javadoc)
	 * @see br.com.livraria.dao.EditoraDao#consultarPorNome(java.lang.String)
	 */
	@Override
	public List<Editora> consultarPorNome(String nome) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Editora> listarTodos() {
		List<Editora> listaEditoras = new ArrayList<Editora>();
		try {
			Connection con = open();
			Statement statement = con.createStatement();
			ResultSet rs = statement.executeQuery("SELECT * FROM editora");
			
			while (rs.next()) {
				Editora a = new Editora();
				a.setId(rs.getInt("id"));
				a.setNome(rs.getString("nome"));
				listaEditoras.add(a);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		
		return listaEditoras;
	}


}
